//package com.rainy.cloud.producer.config;
//
//import com.rainy.cloud.producer.mapper.BaseMapper;
//import com.zaxxer.hikari.HikariDataSource;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.springframework.aop.Advisor;
//import org.springframework.aop.aspectj.AspectJExpressionPointcut;
//import org.springframework.aop.support.DefaultPointcutAdvisor;
//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.env.Environment;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.core.io.support.ResourcePatternResolver;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.transaction.PlatformTransactionManager;
//import org.springframework.transaction.TransactionDefinition;
//import org.springframework.transaction.interceptor.*;
//import tk.mybatis.spring.mapper.MapperScannerConfigurer;
//
//import javax.sql.DataSource;
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.Map;
//
////@MapperScan({"com.rainy.cloud.producer.mapper.auto","com.rainy.cloud.producer.mapper.write"})
//@Configuration
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
//public class MybatisConfig {
//
//    @Bean
//    public DataSource dataSource(){
//        HikariDataSource dataSource = new HikariDataSource();
//        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//        dataSource.setJdbcUrl("jdbc:mysql://192.168.38.129:3306/cloud_user");
//        dataSource.setUsername("root");
//        dataSource.setPassword("Aa123456");
//        return dataSource;
//    }
//
//    @Bean(name="mapperHelper")
//    public MapperScannerConfigurer mapperHelper(){
//        MapperScannerConfigurer scan = new MapperScannerConfigurer();
//        scan.setSqlSessionFactoryBeanName("sqlSessionFactory");
//        scan.setBasePackage("com.rainy.cloud.producer.mapper.auto,com.rainy.cloud.producer.mapper.write");
//        scan.setMarkerInterface(BaseMapper.class);
//        return scan;
//    }
//
//    @Bean(name = "sqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactoryBean() {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        bean.setDataSource(dataSource());
//        bean.setTypeAliasesPackage("com.rainy.cloud.producer.model");
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        try {
//            bean.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/*.xml"));
//            return bean.getObject();
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Bean
//    public PlatformTransactionManager transactionManager() {
//        return new DataSourceTransactionManager(dataSource());
//    }
//
//    @Bean
//    public TransactionInterceptor txAdvice() {
//        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
//        /*只读事务，不做更新操作*/
//        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
//        readOnlyTx.setReadOnly(true);
//        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED );
//        /*当前存在事务就使用当前事务，当前不存在事务就创建一个新的事务*/
//        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
//        requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
//        requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//        requiredTx.setTimeout(5);
//        Map<String, TransactionAttribute> txMap = new HashMap<>();
//        txMap.put("add*", requiredTx);
//        txMap.put("save*", requiredTx);
//        txMap.put("insert*", requiredTx);
//        txMap.put("update*", requiredTx);
//        txMap.put("delete*", requiredTx);
//        txMap.put("get*", readOnlyTx);
//        txMap.put("query*", readOnlyTx);
//        source.setNameMap( txMap );
//        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager(), source);
//        return txAdvice;
//    }
//
//    @Bean
//    public Advisor txAdviceAdvisor() {
//        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
//        pointcut.setExpression("execution (* com.rainy.cloud.producer..service.*.*(..))");
//        return new DefaultPointcutAdvisor(pointcut, txAdvice());
//    }
//}
